package com.docmanage.web.controller.am;

import com.docmanage.am.service.impl.AmBrowseService;
import com.docmanage.common.core.controller.BaseController;
import com.docmanage.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/***
 * @des 档案模块，权限校验控制器
 * @author Ly
 * @date 2023/7/11
 */
@RestController
@RequestMapping("/am/amBrowse")
public class AmBrowseController extends BaseController {

    @Autowired
    private AmBrowseService amBrowseService;

    /****
     * 查看当前登录用户，是否有某些权限
     * id:文件夹id或者是文件id
     * type:folder,file,文件夹还是文件
     * operate:view,add,update,delete，查看，新增，更新，删除
     * @param auth
     * @return
     */
    @RequestMapping("/ifHaveAuth")
    public AjaxResult ifHaveAuth(@RequestBody Map auth){
        boolean ifHave = true;
        if(ifHave){
            return success("权限校验通过");//success("权限校验通过",ifHave);
        }else{
            return error("您没有xxxxx权限");//error("您没有xxxxx权限",ifHave);
        }
    }


    /**
     * 获取档案文件详细信息
     */
    @GetMapping(value = "/viewFile/{id}")
    public AjaxResult viewFile(@PathVariable("id") Long id)
    {
        return AjaxResult.success(amBrowseService.viewFile(id));
    }


    /****
     * 删除文件档案
     * @param id
     */
    @GetMapping(value = "/preview/{id}")
    public AjaxResult preview(@PathVariable("id")Long id){
        String url = amBrowseService.preview(id);
        return AjaxResult.success("成功",url);
    }

    /**
     * 获取文件夹详细信息
     */
    @GetMapping(value = "/viewFolder/{id}")
    public AjaxResult viewFolder(@PathVariable("id") Long id)
    {
        return AjaxResult.success(amBrowseService.viewFolder(id));
    }

    /****
     * 新建文件夹
     * @param data
     * @return
     */
    @PostMapping(value = "/addFolder")
    public AjaxResult addFolder(@RequestBody Map data)
    {
        String name = data.get("name").toString();
        Long parentId = Long.parseLong(data.get("parentId").toString());
        amBrowseService.addFolder(name,parentId);
        return AjaxResult.success();
    }

    /****
     * 重命名文件夹
     * @param data
     * @return
     */
    @PostMapping(value = "/renameFolder")
    public AjaxResult renameFolder(@RequestBody Map data)
    {
        String name = data.get("name").toString();
        Long id = Long.parseLong(data.get("id").toString());
        amBrowseService.renameFolder(name,id);
        return AjaxResult.success();
    }


    /****
     * 重命名文件档案
     * @param data
     * @return
     */
    @PostMapping(value = "/renameFile")
    public AjaxResult renameFile(@RequestBody Map data)
    {
        String name = data.get("name").toString();
        Long id = Long.parseLong(data.get("id").toString());
        amBrowseService.renameFile(name,id);
        return AjaxResult.success();
    }

    /****
     * 移动文件夹
     * @param data
     */
    @PostMapping(value = "/moveFolder")
    public AjaxResult moveFolder(@RequestBody Map data){
        Long id = Long.parseLong(data.get("id").toString());
        Long targetFolderId = Long.parseLong(data.get("targetFolderId").toString());
        amBrowseService.moveFolder(id,targetFolderId);
        return AjaxResult.success();
    }


    /****
     * 移动文件档案
     * @param data
     */
    @PostMapping(value = "/moveFile")
    public AjaxResult moveFile(@RequestBody Map data){
        Long id = Long.parseLong(data.get("id").toString());
        Long targetFolderId = Long.parseLong(data.get("targetFolderId").toString());
        amBrowseService.moveFile(id,targetFolderId);
        return AjaxResult.success();
    }

    /****
     * 删除文件夹
     * @param id
     */
    @PostMapping(value = "/deleteFolder/{id}")
    public AjaxResult deleteFolder(@PathVariable("id")Long id){
        amBrowseService.deleteFolder(id);
        return AjaxResult.success();
    }

    /****
     * 删除文件档案
     * @param id
     */
    @PostMapping(value = "/deleteFile/{id}")
    public AjaxResult deleteFile(@PathVariable("id")Long id){
        amBrowseService.deleteFile(id);
        return AjaxResult.success();
    }

}
